<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		hasEditor,
		getEditorTitle,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				userCacheKey:"hr-kq-tsupplementAttendance-user-cache"
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			formSubmit(){
				let _that = this
				_that.formdata.newFileFlag = true
				if(_that.edit){
					_that.formdata.newFileFlag = false
						if(_that.formdata.formUpload.length > 1){
							for(let index in _that.formdata.formUpload){
									if(!_that.formdata.formUpload[index].hasOwnProperty('id')){
										_that.formdata.newFileFlag = true
									}
								}
						}else if(_that.formdata.formUpload.length <= 1){
							_that.formdata.newFileFlag = true
						}
					_that.formdata.formUpload = JSON.stringify(_that.formdata.formUpload)
					_that.om.formSave("wxSupplementAttendance/save",_that.formdata,_that)
				}else{
					_that.om.formSave("wxSupplementAttendance/save",_that.formdata,_that)
				}
			},
			verifyDataInfo(){
				let _that = this
				if(!_that.$refs.autoform.formdata.userId){
					let title = _that.$refs.autoform.getEditorTitle("userInfo","userId")
					_that.om.toast("请先选择"+title)
					return false
				}else{
					return true
				}
			},
			getWorkParam(){
				let _that = this
				return {
					userId:_that.$refs.autoform.formdata.userId
				}
			},
			getUserSalaryPosInfo(id){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/getUserSalaryPosInfo",
					{
						userId:id
					}
				).then(res=>{
					if(res.succeed){
						_that.$refs.autoform.formdata.salarycatId = res.value.salaryCatId
						_that.$refs.autoform.formdata.salaryCatName = res.value.salaryCatName
						_that.$refs.autoform.formdata.parentUserName  =  res.value.parentUserName
					}
				})
			},
			initLoad(options){
				let _that = this
				let ajaxArr = []
				let fileId = options.fileId
				if(fileId){
					ajaxArr.push({
						url:"tsupplementAttendance/getInfo",
						param:{
							id:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor('topinfo','zuserId',{
						defaultName:_that.userName,
						defaultValue:_that.userId
					},null, mainData);
					changeEditor("userInfo","userId",{
						link:"../../public/pageSearch/pageSearch",
						"linkParam":{
							other: JSON.stringify({
							}),
							namefield:urlUtil.findUserAsSameInst().namefield,
							extrafield:urlUtil.findUserAsSameInst().extrafield,
							searchfield:"q",
							initload:true,
							url:urlUtil.findUserAsSameInst().url ,
							cachename:_that.userCacheKey,
							placeholder:"请输入姓名"
						}
					},null,mainData)
					if(_that.subEditorObj["dataInfo"]){
						let att = {
							getWorkParam:"getWorkParam"
						}
						let configData = _that.subConfigData["dataInfo"]
						if(hasEditor("dataInfo","supplementDate",configData)){
							let title = getEditorTitle("dataInfo","supplementDate",configData)
							att.titleTemplate = title+":<%=supplementDate || '未填写'%>"
						}
						changeEditor('dataInfo','dataStr',att,mainData)
					}
					changeEditor('fileInfo','formUpload','fileUploadKey',"32",mainData)
					if(res[0][0]){
						changeEditor('repairInfo','reasonId',{
							defaultName:res[0][0].dicname,
							defaultValue:res[0][0].id
						},null,mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[0].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[0].value[index])
								_that.$set(_that.formdata,index,res[0].value[index])
							}
						}
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"formUpload",res[0].value.fileList)
						_that.formdata.formUpload = res[0].value.fileList
						
						changeEditor('fileInfo','formUpload',"fileUploadKey","32",mainData)
						if(_that.formdata.formUpload){
							var tempFileList = []
							var fileUrlList = _that.formdata.formUpload.map(t=>{
								tempFileList.push(t.fileSaveName)
								return t.fileSaveName
							})
							changeEditor("fileInfo","formUpload",'fileUrlList',fileUrlList,mainData)
							changeEditor("fileInfo","formUpload","tempFileList",tempFileList,mainData)
						}
						
						 let dataList = _that._.cloneDeep(res[0].value.dataList)
						 _that.subEditorObj["dataInfo"].workList = dataList
						 _that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(dataList))
						 _that.$set(_that.formdata,"dataStr",JSON.stringify(dataList))
					}
					
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for(let fileId in _that.subEditorObj){
					let editor = _that.subEditorObj[fileId]
					uni.getStorage({
						key:getWorkCacheKey(fileId),
						success:(res)=>{
							editor.workList = res.data
							uni.removeStorage({
								key:getWorkCacheKey(fileId)
							})
						}
					})
				}
				uni.getStorage({
					key: _that.userCacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.userId = d.id
							formdata.userName = d.username
							formdata.salaryPosId = d.salaryPosId
							formdata.salaryPosName = d.salaryPosName
							formdata.instId = d.salaryPosInstId
							_that.getUserSalaryPosInfo(d.id)
							formdata.salaryInstName = d.salaryPosInstName
							formdata.parentInstName = d.parentInstName
							uni.removeStorage({
								key: _that.userCacheKey
							});
						}
					}
				});
			}
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
